package com.evil.common.application.enums.judge;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * 条件源类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum JudgeSourceTypeEnum {

    /**
     * 条件源类型（1-流程网关节点，2-流程机器人节点筛选数据）
     */
    PROCESS_GATEWAY_NODE(1, "流程网关节点"),
    PROCESS_ROBOT_NODE_SEARCH(2, "流程机器人节点筛选数据"),
    CONTROL_RELATED_FILTER_CONDITION(3, "控件关联筛选条件"),
    REPORTING_FILTER_CONDITION(4, "报表筛选条件"),
    APPLICATION_ROBOT_DATA_RANGE(5, "应用机器人数据范围"),
    APPLICATION_ROBOT_FILTER_CONDITION(6, "应用机器人目标应用筛选数据"),
    APPLICATION_BUTTON_FILTER_CONDITION(7, "应用按钮筛选数据"),
    APPLICATION_BUTTON_TRIGGER_CONDITION(8, "应用按钮触发条件"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<JudgeSourceTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static JudgeSourceTypeEnum isExistById(Integer id) {
        return JudgeSourceTypeEnum.findById(id).orElseThrow(JudgeSourceTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<JudgeSourceTypeEnum> consumer) {
        Arrays.asList(values()).forEach(consumer);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "条件源类型参数错误");
    }
}
